package com.example.yichuangyun.mvvm.ui.activity.view;

import androidx.lifecycle.MutableLiveData;

import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.model.contacts.AddressDataModel;
import com.example.yichuangyun.model.contacts.ContactsParamsModel;
import com.example.yichuangyun.model.contacts.ContactsTagModel;
import com.example.yichuangyun.model.business.LabelModel;
import com.example.yichuangyun.mvvm.base.BaseViewModel;
import com.example.yichuangyun.mvvm.di.retrofit.BaseModel;
import com.example.yichuangyun.mvvm.di.retrofit.BaseNetCallback;
import com.example.yichuangyun.utils.treelist.SetShareBean;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import io.reactivex.subscribers.ResourceSubscriber;

public class AddressBookViewModel extends BaseViewModel {

    public final MutableLiveData<AddressDataModel> contactsData = new MutableLiveData<>();

    public final MutableLiveData<SetShareBean> treeData = new MutableLiveData<>();

    public final MutableLiveData<List<ContactsTagModel>> tagsData = new MutableLiveData<>();

    public final MutableLiveData<List<LabelModel>> labelsData = new MutableLiveData<>();

    public final MutableLiveData<List<LabelModel>> contactSourceData = new MutableLiveData<>();

    /**
     * 获取联系人列表
     */
    public void getContactsList(int page, ContactsParamsModel contactsParamsModel) {
        contactsParamsModel.setPage(page);
        contactsParamsModel.setPageSize(Constant.pageSize);
        addSubscribe(mRetrofitHelper.getContactsList(new Gson().toJson(contactsParamsModel)).subscribeWith(new BaseNetCallback<AddressDataModel>() {

            @Override
            public void onSuccess(BaseModel<AddressDataModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    contactsData.setValue(baseModel.getData());
                } else {
                    mErrorData.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                mErrorData.setValue(message);
            }
        }));
    }

    /**
     * 联系人来源
     */
    public void getContactSource() {
        addSubscribe(mRetrofitHelper.getContactsSource().subscribeWith(new BaseNetCallback<List<LabelModel>>() {
            @Override
            public void onSuccess(BaseModel<List<LabelModel>> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (baseModel.getData() != null) {
                        contactSourceData.setValue(baseModel.getData());
                    }
                }
            }
        }));
    }

    /**
     * 获取标签
     */
    public void getTag() {
        JSONObject requestData = new JSONObject();
        addSubscribe(mRetrofitHelper.getTags(requestData).subscribeWith(new BaseNetCallback<List<ContactsTagModel>>() {
            @Override
            public void onSuccess(BaseModel<List<ContactsTagModel>> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    tagsData.setValue(baseModel.getData());
                }
            }
        }));
    }

    /**
     * 联系人类型
     */
    public void getContactsType() {
        addSubscribe(mRetrofitHelper.getContactsType().subscribeWith(new BaseNetCallback<List<LabelModel>>() {
            @Override
            public void onSuccess(BaseModel<List<LabelModel>> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    labelsData.setValue(baseModel.getData());
                }
            }
        }));
    }


    public void getTreeData(String keyword) {
        JSONObject requestData = new JSONObject();
        try {
            requestData.put("keyword", keyword);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        addSubscribe(mRetrofitHelper.getTreeData(requestData).subscribeWith(new ResourceSubscriber<SetShareBean>() {

            @Override
            public void onNext(SetShareBean setShareBean) {
                if (Constant.SUCCESS == setShareBean.getResult()) {
                    treeData.setValue(setShareBean);
                }
            }

            @Override
            public void onError(Throwable t) {
                mErrorData.setValue(t.getMessage());
            }

            @Override
            public void onComplete() {

            }
        }));
    }
}
